<template>
  <BasicTable @register="registerTable">
    <template #tableTitle>
      <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleCreate"> 新增</a-button>
    </template>
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
  <FileConfigDrawer @register="registerDrawer" @success="reload" :showFooter="showFooter" />
  <FileConfigDesc @register="registerDesc" />
</template>
<script lang="ts" name="FileConfig" setup>
  import { ref } from "vue"
  import { BasicTable, TableAction } from "/@/components/Table"
  import { useDrawer } from "/@/components/Drawer"
  import FileConfigDrawer from "./form.vue"
  import FileConfigDesc from "./detail.vue"
  import { columns, searchFormSchema } from "./data"
  import * as apis from "/@/api/infra/file"
  import { useListPage } from "/@/hooks/system/useListPage"
  import { Icon } from "/@/components/Icon"

  const showFooter = ref(true)
  const [registerDrawer, { openDrawer }] = useDrawer()
  const [registerDesc, { openDrawer: openFileConfigDesc }] = useDrawer()

  const { prefixCls, tableContext } = useListPage({
    designScope: "file-config",
    tableProps: {
      title: "文件配置列表",
      api: apis.pageFileConfig,
      bordered: false,
      columns: columns,
      ellipsis: false,
      actionColumn: {
        width: 140,
        title: "操作",
        dataIndex: "action",
        fixed: "right",
        slots: { customRender: "action" },
      },
      formConfig: {
        schemas: searchFormSchema,
      },
    },
  })
  const [registerTable, { reload }, { }] = tableContext

  function handleCreate() {
    showFooter.value = true
    openDrawer(true, {
      isUpdate: false,
    })
  }

  function handleEdit(record: Recordable) {
    showFooter.value = true
    openDrawer(true, {
      record,
      isUpdate: true,
    })
  }

  function handleDetail(record) {
    showFooter.value = false
    openFileConfigDesc(true, {
      record,
      isUpdate: true,
    })
  }

  async function handleDelete(record) {
    await apis.deleteFileConfig({ id: record.id }, reload)
  }

  function getTableAction(record) {
    return [
      {
        label: "删除",
        color: "error",
        popConfirm: {
          title: "是否确认删除",
          confirm: handleDelete.bind(null, record),
        },
      },
      {
        label: "编辑",
        onClick: handleEdit.bind(null, record),
      },
      {
        label: "详情",
        onClick: handleDetail.bind(null, record),
      },
    ]
  }
</script>

<style></style>
